package com.jshan.blog.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshan.blog.annotation.OperationLog;
import com.jshan.blog.base.admin.dto.SysRoleResDto;
import com.jshan.blog.enums.BizCodeEnums;
import com.jshan.blog.result.Result;
import com.jshan.blog.service.SysRoleService;
import com.jshan.blog.valid.AddGroupValid;
import com.jshan.blog.valid.UpdateGroupValid;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/role")
@Tag(name = "角色信息相关")
@Slf4j
public class RoleController {

    @Resource
    private SysRoleService roleService;

    @OperationLog(value = "新增角色", operationType = "新增")
    @Operation(summary = "新增角色")
    @SaCheckPermission(value = {"sys:role:add"})
    @PostMapping("/addRole")
    public Result addRole(@Validated(value = {AddGroupValid.class}) @RequestBody SysRoleResDto sysRoleResDto) {
        if (roleService.saveRoleDto(sysRoleResDto)) {
            return Result.ok(BizCodeEnums.SUCCESS.getCode(), "新增角色成功");
        }
        return Result.fail(BizCodeEnums.INSERT_FAILE.getCode(), BizCodeEnums.INSERT_FAILE.getMsg());
    }

    @OperationLog(value = "修改角色", operationType = "修改")
    @Operation(summary = "修改角色")
    @SaCheckPermission(value = {"sys:role:edit"})
    @PutMapping("/updateRole")
    public Result update(@Validated(value = {UpdateGroupValid.class}) @RequestBody SysRoleResDto sysRoleResDto) {
        if (roleService.updateDto(sysRoleResDto)) {
            return Result.ok(BizCodeEnums.SUCCESS.getCode(), "修改角色信息成功");
        }
        return Result.fail(BizCodeEnums.UPDATE_FAILE.getCode(), "修改角色信息失败");
    }

    @OperationLog(value = "删除角色", operationType = "删除")
    @Operation(summary = "删除角色")
    @SaCheckPermission(value = {"sys:role:del"})
    @DeleteMapping("/deleteRole/{id}")
    public Result delRole(@PathVariable("id") Integer id) {
        return roleService.removeRole(id);
    }

    /**
     * 获取角色列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @OperationLog(value = "查询角色列表", operationType = "查询")
    @SaCheckPermission(value = {"sys:role:manage"})
    @GetMapping("/getRoles/{page}/{pageSize}")
    public Result getRoles(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
        Page<SysRoleResDto> pageList = roleService.pageAll(new Page<SysRoleResDto>(page, pageSize));
        Map<Object, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(pageList.getRecords())) {
            map.put("records", pageList.getRecords());
            map.put("total", pageList.getTotal());
            return Result.ok(map);
        }
        return Result.fail(BizCodeEnums.FAILE.getCode(), "获取角色信息失败！");
    }

    /**
     * 角色授权菜单接口
     * @param ids 菜单ids
     * @param roleId 角色id
     * @return result
     */
    @OperationLog(value = "角色授权菜单权限", operationType = "新增or更新")
    @SaCheckPermission(value = {"sys:role:auth"})
    @PostMapping("/authRoleList/{roleId}")
    public Result authRoleList(@RequestBody Set<Integer> ids,
                               @PathVariable("roleId") Integer roleId) {
        if (roleService.addMenuIds(roleId, ids)){
            return Result.ok(BizCodeEnums.SUCCESS.getCode(), "授权成功");
        }
        return Result.fail(BizCodeEnums.FAILE.getCode(), "授权失败");
    }
}
